﻿package com.wandian.view.mediator
{
	import com.wandian.controller.CLZNoticeName;
	import com.wandian.data.GameData;
	import com.wandian.model.InRoomPlayerDataProxy;
	import com.wandian.model.vo.MessageVo;
	import com.wandian.model.vo.PlayerVo;
	import com.wandian.model.vo.PropVo;
	import com.wandian.util.CLZButton;
	import com.wandian.util.CLZLlistScrollPanel;
	import com.wandian.util.CLZLoadingData;
	import com.wandian.util.CLZTabBar;
	import com.wandian.view.components.ui.AddBtn;
	import com.wandian.view.components.ui.GamePageMessageInfoMc;
	import com.wandian.view.components.ui.Player;
	import com.wandian.view.components.ui.SoundManage;
	import com.wandian.view.components.ui.SystemMessage;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	
	import org.puremvc.as3.interfaces.*;
	import org.puremvc.as3.patterns.mediator.*;

    public class GameMessageMediator extends Mediator
    {
        private var _tabBar:CLZTabBar;
        private var _nInitShowType:int;
        private var _arrListData:Array;
        private var _mcConten:Sprite;
        private var _messageId:int;
        public static const NAME:String = "GameMessageMediator";

        public function GameMessageMediator(viewComponent:Object, initShowType:int, messageId:int)
        {
            this._nInitShowType = initShowType;
            this._messageId = messageId;
            super(NAME, viewComponent);
            return;
        }

        override public function onRegister() : void
        {
            this.mcPanel["title_txt"].text = GameData.tip["SYSTEM_MESSAGE"];
            this._mcConten = new Sprite();
            this.mcPanel.addChild(this._mcConten);
            this._mcConten.x = 25;
            this._mcConten.y = 83;
            AddBtn.initBtn(this.mcPanel, CLZNoticeName.HIDE_GAME_MESSAGE_PANEL, AddBtn.CLOSE_TWO, "", 4, 1);
//            var tabs:Array = [[this.mcPanel.messageBtn1, GameData.tip["SYSTEM_MESSAGE_GAME"]], [this.mcPanel.messageBtn2, GameData.tip["GAME_MESSAGE_GAME"]], [this.mcPanel.messageBtn3, GameData.tip["INDEX_MESSAGE_GAME"]]];
			this.mcPanel.messageBtn2.visible=false;
			this.mcPanel.messageBtn3.visible=false;
			var tabs:Array = [[this.mcPanel.messageBtn1, GameData.tip["SYSTEM_MESSAGE_GAME"]]];
            this._tabBar = new CLZTabBar(tabs);
            var index:int = 0;
            while (index < tabs.length)
            {
                
                tabs[index][0].addEventListener(Event.CHANGE, this.changeTabBarHandler);
                index = index + 1;
            }
            this.sendNotification(CLZNoticeName.MESSAGE_SYSTEM_REQ, false);
            CLZLoadingData.show(this._mcConten, 682, 410);
            return;
        }

        private function changeTabBarHandler(event:Event) : void
        {
            var mc:MovieClip = null;
            var _loc_3:SystemMessage = null;
            GameData.removeNumChildre(this._mcConten);
            switch(event.target.name)
            {
                case "messageBtn1":
                {
                    mc = GameData.getMovieClip("SystemMessage");
                    this._mcConten.addChild(mc);
                    if (this._arrListData[0].length == 0)
                    {
                        return;
                    }
                    _loc_3 = new SystemMessage(mc, this._arrListData[0], this._messageId);
                    mc.x = 0;
                    mc.y = 0;
                    this._messageId = 0;
                    break;
                }
                case "messageBtn2":
                {
                    this.showMessageList(this._arrListData[1], GameData.GMAE_MESSAGE);
                    break;
                }
                case "messageBtn3":
                {
                    this.showMessageList(this._arrListData[2], GameData.PAGE_MESSAGE);
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }

        private function showMessageList(param1:Array, param2:int) : void
        {
            var _loc_6:MovieClip = null;
            var _loc_7:MessageVo = null;
            var _loc_8:GamePageMessageInfoMc = null;
            if (param1 != null)
            {
            }
            if (param1.length == 0)
            {
                return;
            }
            var _loc_3:* = new CLZLlistScrollPanel(1, 719, 123, 0, 5, 410, 682);
            this._mcConten.addChild(_loc_3);
            _loc_3.x = 5;
            _loc_3.y = 5;
            var _loc_4:* = param1.length;
            var _loc_5:int = 0;
            while (_loc_5 < _loc_4)
            {
                
                _loc_6 = GameData.getMovieClip("MessageInfoMc");
                _loc_3.setMc(_loc_6, _loc_5, _loc_4);
                if (param2 == GameData.GMAE_MESSAGE)
                {
                    _loc_6.goto_btn.visible = false;
                }
                else
                {
                    CLZButton.setButton(_loc_6.goto_btn, GameData.tip["PARTICIPATE"]);
                }
                _loc_7 = param1[_loc_5] as MessageVo;
                trace("messageVo.imgURL:" + _loc_7.imgURL);
                _loc_8 = new GamePageMessageInfoMc(_loc_6, param1[_loc_5]);
                _loc_5 = _loc_5 + 1;
            }
            return;
        }

        private function removeAll() : void
        {
            var _loc_1:* = undefined;
            while (this._mcConten.numChildren)
            {
                
                _loc_1 = this._mcConten.removeChildAt(0);
                _loc_1 = null;
            }
            return;
        }

        override public function listNotificationInterests() : Array
        {
            return [CLZNoticeName.HIDE_GAME_MESSAGE_PANEL, CLZNoticeName.MESSAGE_SYSTEM_RES];
        }

        override public function handleNotification(notification:INotification) : void
        {
            var arrMsgVo:Array = null;
            var index:int = 0;
            var msgVo:MessageVo = null;
            switch(notification.getName())
            {
                case CLZNoticeName.HIDE_GAME_MESSAGE_PANEL:
                {
                    facade.removeMediator(GameMessageMediator.NAME);
                    Texas.I._mcWindosView.removeChildTo(this.mcPanel);
                    viewComponent = null;
                    break;
                }
                case CLZNoticeName.MESSAGE_SYSTEM_RES:
                {
                    CLZLoadingData.hide(this._mcConten);
                    this._arrListData = [[], [], []];
                    arrMsgVo = notification.getBody() as Array;
                    index = 0;
                    while (index < arrMsgVo.length)
                    {
                        
                        msgVo = arrMsgVo[index] as MessageVo;
                        if (msgVo.type == GameData.SYSTEM_MESSAGE)
                        {
                            this._arrListData[0].push(msgVo);
                        }
                        else if (msgVo.type == GameData.GMAE_MESSAGE)
                        {
                            this._arrListData[1].push(msgVo);
                        }
                        else
                        {
                            this._arrListData[2].push(msgVo);
                        }
                        index = index + 1;
                    }
                    switch(this._nInitShowType)
                    {
                        case GameData.SYSTEM_MESSAGE:
                        {
                            this._tabBar.setClickIndex(this.mcPanel.messageBtn1);
                            break;
                        }
                        case GameData.GMAE_MESSAGE:
                        {
                            this._messageId = 0;
                            this._tabBar.setClickIndex(this.mcPanel.messageBtn2);
                            break;
                        }
                        case GameData.PAGE_MESSAGE:
                        {
                            this._messageId = 0;
                            this._tabBar.setClickIndex(this.mcPanel.messageBtn3);
                            break;
                        }
                        default:
                        {
                            break;
                        }
                    }
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }

        private function get mcPanel() : MovieClip
        {
            return viewComponent as MovieClip;
        }

    }
}
